declare let eManager;
const {ccclass, property} = cc._decorator;

//4-1 美瞳
@ccclass
export default class MeiTong extends cc.Component {
    @property
    stepName = "";
    //盒子动画（入场，右眼，左眼）
    @property(cc.Animation)
    boxAni:cc.Animation = null;
    //左眼美瞳动画
    @property(cc.Animation)
    leftAni:cc.Animation = null;
    //右眼美瞳动画
    @property(cc.Animation)
    rightAni:cc.Animation = null;
    //美瞳 左 
    @property(cc.Node)
    meiTongLeft: cc.Node = null;
    //美瞳 右
    @property(cc.Node)
    meiTongRight: cc.Node = null;
    //美瞳碰撞区 左
    @property(cc.Node)
    pengLeftNode: cc.Node = null;
    //美瞳碰撞区 右
    @property(cc.Node)
    pengRightNode: cc.Node = null;

    //美瞳效果 右
    @property(cc.Node)
    effectRightNode: cc.Node = null;
    //美瞳效果 左
    @property(cc.Node)
    effectLeftNode: cc.Node = null;
    //音效
    @property({type:cc.AudioClip})
    sound: cc.AudioClip = null;

    //根节点 动画
    @property(cc.Animation)
    rootAni:cc.Animation = null;

    
    _isPlay = false;
    _curPoint = 1;//一共2步  先是右眼 再是左眼

    start(){
        //@ts-ignore
        this.boxAni.on("finished",this.startFinish,this);
        //@ts-ignore
        this.rightAni.on("finished",this.rightFinish,this);
        //@ts-ignore
        this.leftAni.on("finished",this.leftFinish,this);
        //@ts-ignore
        this.rootAni.on("finished",this.rootFinish,this);
    };
    //初始化
    initData(){
        this._curPoint = 1;
        // eManager.emit("HZ_SHOW_SELECT_LIST", {
        //     step: 4,
        //     point: 1,
        //     callback:()=>{
        //         this.rootAni.play("step4_1_01");
        //         this.boxAni.play("step4_1mth_jinru");
        //     }
        // });
        this.rootAni.play("step4_1_01");
        this.boxAni.play("step4_1mth_jinru");
        this._isPlay = true;
        eManager.emit("REFRESH_STEP_NAME", this.stepName);
        eManager.emit("HZ_REFRESH_ICON_TIPS", "step_4_1");
    };

    _touchStart(event){
        if(this._isPlay)
            return;
        if(this._curPoint > 2)
            return;

        let worldPoint = event.touch.getLocation();
        if(this._curPoint == 1){
            let localPoint = this.meiTongRight.parent.convertToNodeSpaceAR(worldPoint);
            this.meiTongRight.x = localPoint.x;
            this.meiTongRight.y = localPoint.y;
        }else{
            let localPoint = this.meiTongLeft.parent.convertToNodeSpaceAR(worldPoint);
            this.meiTongLeft.x = localPoint.x;
            this.meiTongLeft.y = localPoint.y;
        }
    };

    _touchMove(event:any){
        if(this._isPlay)
            return;
        if(this._curPoint > 2)
            return;
        // let delta = event.touch.getDelta();
        // if(this._curPoint == 1){
        //     this.meiTongRight.x += delta.x;
        //     this.meiTongRight.y += delta.y;
        // }else{
        //     this.meiTongLeft.x += delta.x;
        //     this.meiTongLeft.y += delta.y;
        // }
        let worldPoint = event.touch.getLocation();
        if(this._curPoint == 1){
            let localPoint = this.meiTongRight.parent.convertToNodeSpaceAR(worldPoint);
            this.meiTongRight.x = localPoint.x;
            this.meiTongRight.y = localPoint.y;
        }else{
            let localPoint = this.meiTongLeft.parent.convertToNodeSpaceAR(worldPoint);
            this.meiTongLeft.x = localPoint.x;
            this.meiTongLeft.y = localPoint.y;
        }
        let peng = this._curPoint == 1 ? this.pengRightNode.getBoundingBoxToWorld() :this.pengLeftNode.getBoundingBoxToWorld();
        let box = this._curPoint == 1 ? this.meiTongRight.getBoundingBoxToWorld() : this.meiTongLeft.getBoundingBoxToWorld();
        if (peng.intersects(box)) {
            this.playAni();
        }
    };

    playAni(){
        this._isPlay = true;
        if(this._curPoint == 1){
            this.rightAni.play("step4_1meitongR_anzhuang");
            cc.audioEngine.play(this.sound, false, 1);
            eManager.emit("SET_VIBRATE", 500);
        }else if(this._curPoint == 2){
            this.leftAni.play("step4_1meitongL_anzhuang");
            cc.audioEngine.play(this.sound, false, 1);
            eManager.emit("SET_VIBRATE", 500);
        }
    };
    //盒子动画完成
    startFinish(event: cc.Event.EventCustom, animState: cc.AnimationState) {
        if (animState.name == "step4_1mth_jinru") {
            this.pengRightNode.active = true;
            this.boxAni.play("step4_1mth_R");//右盒子飞入
        } else if (animState.name == "step4_1mth_R") {
            this.meiTongRight.active = true;
            this.rightAni.play("step4_1meitongR");//美瞳位移
        } else if (animState.name == "step4_1mth_L") {
            this.meiTongLeft.active = true;
            this.leftAni.play("step4_1meitongL");//美瞳位移
        }
    };
    //右美瞳动画
    rightFinish(event: cc.Event.EventCustom, animState: cc.AnimationState){
        if (animState.name == "step4_1meitongR"){
            this._isPlay = false;
        }else if (animState.name == "step4_1meitongR_anzhuang"){
            this.pengRightNode.active = false;
            this.pengLeftNode.active = true;
    
            this.meiTongRight.active = false;
            this.effectRightNode.active = true;
            this.rootAni.play("step4_1_02");
        }
    };
    //左美瞳动画
    leftFinish(event: cc.Event.EventCustom, animState: cc.AnimationState){
        if (animState.name == "step4_1meitongL"){
            this._curPoint ++;
            this._isPlay = false;
        }else if (animState.name == "step4_1meitongL_anzhuang"){
            this.pengLeftNode.active = false;
        
            this.meiTongLeft.active = false;
            this.effectLeftNode.active = true;
            this.boxAni.node.active = false;
            this.rootAni.play("step4_1_03");
            eManager.emit("DONE_POINT", 1);
            eManager.emit("MINI_DONE_POINT", 1);
            eManager.emit("HZ_MAKE_DOT", {
                makeup_close:4,
                makeup_step:this.stepName
            });
        }
    };

    rootFinish(event: cc.Event.EventCustom, animState: cc.AnimationState){
        if(animState.name == "step4_1_02"){
            this.boxAni.play("step4_1mth_L");//左盒子飞入
        }
    };
}
